<?php
class NVL_FtpUpload{
    protected $ftp_server;
    protected $username;
    protected $password;
    public function __construct($_ftp_server,$_username,$_password)
    {
         $this->ftp_server = $_ftp_server;
         $this->username = $_username;
         $this->password = $_password;
    }
    public function Upload($path,$name,$file){
        //echo $path.'/'.$name;
        $ftp_conn = ftp_connect($this->ftp_server);
        
        $login = ftp_login($ftp_conn, $this->username, $this->password);
        if(!$ftp_conn || !$login){
            return "0";
        }
        $this->ftp_mksubdirs($ftp_conn, $path);
        // upload file
        @ftp_chdir($ftp_conn, $path.'/');
        try {
            if (ftp_put($ftp_conn, $name, $file, FTP_BINARY))
            {
                ftp_close($ftp_conn);
                return '1';
            }
            else
            {
                ftp_close($ftp_conn);
                return '0';
            }
        } catch (Exception $e) {
            //var_dump($e);
            return 0;
        }
        
        
        // close connection
        
    }
    function ftp_mksubdirs($ftpcon,$ftpath){
        //@ftp_chdir($ftpcon, $ftpbasedir); // /var/www/uploads
        $parts = explode('/',$ftpath); // 2013/06/11/username
        foreach($parts as $part){
            if(!@ftp_chdir($ftpcon, $part)){
                ftp_mkdir($ftpcon, $part);
                ftp_chdir($ftpcon, $part);
                //ftp_chmod($ftpcon, 0777, $part);
            }
        }
    }
}